package com.application.lucky.request;

import com.application.lucky.bean.jsonBean.LoginBean;
import com.application.lucky.request.nav.UrlList;
import com.application.lucky.request.nav.UrlManager;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.baselib.libnetwork.net.libnetworkcomponent.HttpCallback;
import com.baselib.libnetwork.net.libnetworkcomponent.PieBaseRequest;
import com.baselib.libnetwork.net.model.ResultResponse;

import java.util.HashMap;
import java.util.Map;

public class LoginRequest extends PieBaseRequest {
    //登录
    public void login(String uName, String password, final LoginBack<BaseResponse<LoginBean>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("login_name", uName);
        param.put("password", password);
        String url = UrlManager.getUrl(UrlList.login_email);
        post(url, param, new CallBackEx<BaseResponse<LoginBean>>() {
            @Override
            public void onSuccess(BaseResponse<LoginBean> stringBaseResponse) {
                back.success(stringBaseResponse);

            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<LoginBean> stringBaseResponse) {
                back.fail(code);
            }

            @Override
            public void onFailed(int code) {
                back.fail(code);
            }

        });
    }

    //注册
    public void register(String uName, String password, final LoginBack<BaseResponse<LoginBean>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("login_name", uName);
        param.put("password", password);
        String url = UrlManager.getUrl(UrlList.login_register_email);
        post(url, param, new CallBackEx<BaseResponse<LoginBean>>() {
            @Override
            public void onSuccess(BaseResponse<LoginBean> stringBaseResponse) {
                back.success(stringBaseResponse);
            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<LoginBean> stringBaseResponse) {
                back.fail(code);
            }

            @Override
            public void onFailed(int code) {
                if (back != null) {
                    back.fail(code);
                }
            }

            @Override
            public void onException(ResultResponse response) {
                back.fail(response.code());
            }
        });
    }

    public void invite(String userid, final LoginBack<BaseResponse<String>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("user_id", userid);
        String url = UrlManager.getUrl(UrlList.red_user_invitation);
        get(url, param, new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> stringBaseResponse) {
                if (back != null) {
                    back.success(stringBaseResponse);
                }
            }


            @Override
            public void onFailed(int code) {
                if (back != null) {
                    back.fail(code);
                }
            }


        });
    }

    //修改密码
    public void setPassword(String uName, String password, final LoginBack<BaseResponse<String>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("login_name", uName);
        param.put("password", password);
        String url = UrlManager.getUrl(UrlList.login_forget_password);
        post(url, param, new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> stringBaseResponse) {
                back.success(stringBaseResponse);

            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<String> stringBaseResponse) {
                back.fail(code);
            }

            @Override
            public void onFailed(int code) {
                if (back != null) {
                    back.fail(code);
                }
            }

            @Override
            public void onException(ResultResponse response) {
                back.fail(response.code());
            }
        });


    }

    public void loginOut(String uName, final LoginBack<BaseResponse<String>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("login_name", uName);
        String url = UrlManager.getUrl(UrlList.login_logout);
        post(url, param, new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> stringBaseResponse) {
                if (back != null) {
                    back.success(stringBaseResponse);

                }

            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<String> stringBaseResponse) {
                if (back != null)
                    back.fail(code);
            }

            @Override
            public void onFailed(int code) {
                if (back != null) {
                    back.fail(code);
                }
            }

            @Override
            public void onException(ResultResponse response) {
                if (back != null)
                    back.fail(response.code());
            }
        });
    }

    public void login_valid_loginName(String uName, final LoginBack<BaseResponse<String>> back) {
        Map<String, String> param = new HashMap<>();
        param.put("login_name", uName);
        String url = UrlManager.getUrl(UrlList.login_valid_loginName);
        post(url, param, new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> stringBaseResponse) {
                back.success(stringBaseResponse);

            }

            @Override
            protected void OnFailureNeedData(int code, BaseResponse<String> stringBaseResponse) {
                back.fail(code);
            }

            @Override
            public void onFailed(int code) {
                if (back != null) {
                    back.fail(code);
                }
            }

            @Override
            public void onException(ResultResponse response) {
                back.fail(response.code());
            }
        });
    }


    public void getIMToken() {
        post("", new CallBackEx<BaseResponse<String>>() {
            @Override
            public void onSuccess(BaseResponse<String> o) {

            }

            @Override
            public void onFailed(int code) {

            }


        });
    }

    public interface LoginBack<T> {
        void success(T t);

        void fail(int code);
    }
}
